﻿using ProjectFootball.MVC.Foundation;
using ProjectFootball.MVC.Foundation.Repositories;
using ProjectFootball.MVC.Models.EntityFramework.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;

namespace ProjectFootball.MVC.Models.RepositoryProviders
{
    public static class CompetitionRepositoryExtensions
    {
        public static IEnumerable<string> GetAutoCompleteList(this IDataRepository<Competition> competitionRepository, IPrincipal user, string searchText)
        {
            var competitions = from c in competitionRepository
                               where !c.IsMarkedForDeletion 
                               && c.CompetitionName.Contains(searchText)
                               select c;

            return competitions.AsEnumerable().Where(c => c.CanView(user)).Select(c => c.CompetitionName).Distinct().OrderBy(c => c);
        }

        public static IEnumerable<ICodePickerData> GetCodePickerData(this IDataRepository<Competition> competitionRepository)
        {
            return competitionRepository
                .Where(c => c.IsApproved && !c.IsMarkedForDeletion)
                .OrderBy(c => c.CompetitionName)
                .AsEnumerable()
                .Select(c =>
                    new GuidCodePicker()
                    {
                        Code = c.Pk,
                        Description = c.CompetitionName
                    });
        }

        public static Competition SearchByPrimaryKey(this IDataRepository<Competition> competitionRepository, Guid id)
        {
            var competitions = from c in competitionRepository
                               where c.Pk == id
                               select c;

            return competitions.Any() ? competitions.Single() : null;
        }
    }
}